package com.lrkj.back.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lrkj.back.Const;
import com.lrkj.back.entity.ZsEquip;
import com.lrkj.back.entity.ZsEquipCeshi;
import com.lrkj.back.entity.ZsSoft;
import com.lrkj.back.service.TbSeqService;
import com.lrkj.back.service.ZsEquipCeshiService;
import com.lrkj.back.service.ZsEquipService;
import com.lrkj.back.service.ZsSoftService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.cbons.mamisay.parent.exception.BusinessException;

import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/soft")
public class ZsSoftController extends BaseController{
	@Autowired
	private ZsSoftService zsSoftService;
	@Autowired
	private TbSeqService seqService;
	@Autowired
	private ZsEquipService equipService;
	@Autowired
	private ZsEquipCeshiService equipCeshiSercie;

	//配置--程序管理   （点击查询）
	@RequestMapping(value="/softList.do")
	public ModelAndView softList(String searchTxt,HttpServletRequest request,HttpServletResponse response)throws BusinessException{
		//
		//取页面传过来的页面值
		Integer pageNo = request.getParameter("pageIndex")==null? 1 :Integer.valueOf(request.getParameter("pageIndex"));
		
		Map<String,Object> map =new HashMap<>();
		//查找时传过来的值
		if(StringUtils.isNotBlank(searchTxt)){
			searchTxt = searchTxt.trim();
			map.put("softName", searchTxt);
		}
		//
		map.put("pageNo", pageNo);
		map.put("pageSize", Const.PAGE_SIZE);
		PageInfo<ZsSoft> page = zsSoftService.findAllZsSoftList(map);
		//保存下来做什么 的
		//查到的对象集合传到页面
		request.setAttribute("softList", page.getList());
		//返回显示到搜索框
		request.setAttribute("searchTxt", searchTxt==null?"":searchTxt);
		request.setAttribute("total", page.getPages());
		//返回页面
		return new ModelAndView("/peizhi/chengxu/soft_list");
	}
		
	//点添加时，判断传入的id为空不
	@RequestMapping("/softEdit.do")
	public ModelAndView softEdit(String  softId, HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		ZsSoft zsSoft = (softId == null) ? new ZsSoft() : zsSoftService.selectByPrimaryKey(Integer.valueOf(softId));
		Map<String, Object> map = new HashMap<>();
		List<ZsEquip> equips = equipService.findAllZsEquipListNoPage(map);
		request.setAttribute("equips", equips);
		if(softId == null){
			SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMdd");
			softId = sdf.format(new Date())+seqService.seqVal("softno");
			zsSoft.setSoftCode(softId);
		}
		request.setAttribute("zsSoft", zsSoft);
		return new ModelAndView("/peizhi/chengxu/soft_edit");
	}
	@RequestMapping(value="/softSave.do",method=RequestMethod.POST)
	@Transactional
	@ResponseBody
	public Map<Object, Object> softSave(ZsSoft zsSoft)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<>();
		try {

			if(zsSoft.getSoftId() == null ){
				zsSoft.setSoftCreatetime(new Date());
				if(zsSoft.getSoftType()==3){
					Map<String,Object> map = new HashMap<>();
					map.put("equipId",zsSoft.getEquipId());
					map.put("softType",3);
					List<ZsSoft> zsSofts = zsSoftService.findSoftByEquipId(map);
					if(!zsSofts.isEmpty()){
						rtnMap.put("status", "false");
						rtnMap.put("msg", "每个机器只能有一个测试程序");
					}else{
						zsSoftService.insert(zsSoft);
						rtnMap.put("status", "true");
						rtnMap.put("msg", "录入成功");
					}
				}else{
					zsSoftService.insert(zsSoft);
					rtnMap.put("status", "true");
					rtnMap.put("msg", "录入成功");
				}
			}else{
				zsSoftService.updateByPrimaryKey(zsSoft);
				rtnMap.put("status", "true");
				rtnMap.put("msg", "修改成功");
			}

		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "编辑失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
	
	@RequestMapping(value="/softDelete.do")
	@ResponseBody
	@Transactional
	public Map<Object, Object> softDelete(HttpServletRequest request,HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<>();
		try {
			String softId = request.getParameter("softId");
			System.out.println(softId);
			String[] softIds = softId.split(",");
			for(String equId : softIds){
				ZsSoft zsSoft = zsSoftService.selectByPrimaryKey(Integer.valueOf(equId));
				if(zsSoft.getSoftType()!=3){
					zsSoftService.deleteByPrimaryKey(Integer.valueOf(equId));
					rtnMap.put("status", "true");
					rtnMap.put("msg", "删除成功");
				}else{
					rtnMap.put("msg","机器测试程序无法删除");
					break;
				}
			}

		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "删除失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws BusinessException
	 */
	@RequestMapping("/findSoftByEquipId.do")
	@ResponseBody
	public Map<Object, Object> findSoftByEquipId(HttpServletRequest request,HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<>();
		Map<String, Object> map = new HashMap<>();

		try {
			map.put("equipId",request.getParameter("id"));
			map.put("softType", 2);
			List<ZsSoft> lstSofts = zsSoftService.findSoftByEquipId(map);
			rtnMap.put("lstSofts", lstSofts);
			rtnMap.put("status", "true");
			rtnMap.put("msg", "查询成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "查询失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
	
	@RequestMapping("/findCeshiChengxu.do")
	@ResponseBody
	public Map<Object, Object> findCeshiChengxu(HttpServletRequest request,HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<>();
		Map<String, Object> map = new HashMap<>();
		map.put("equipId",request.getParameter("equipId"));
		map.put("softType",3);
		List<ZsSoft> lstSofts = zsSoftService.findSoftByEquipId(map);
		if(lstSofts.size()!=0){
			rtnMap.put("lstSofts", lstSofts);
			rtnMap.put("status", "true");
			rtnMap.put("msg", "查询成功");
		}
		else{
			rtnMap.put("status", "false");
			rtnMap.put("msg", "查询失败");
		}
		return rtnMap;
	}


	
	@RequestMapping("validSoft.do")
	@ResponseBody
	public Map<Object, Object> validSoft(String softId, HttpServletRequest request,HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<>();
		Map<String, Object> map = new HashMap<>();
		try{
			int sid = Integer.valueOf(softId);
			ZsSoft soft = zsSoftService.selectByPrimaryKey(Integer.valueOf(softId));
			if (soft != null) {
				ZsEquip equip = equipService.selectByPrimaryKey(soft.getEquipId());
				if (equip != null) {
					map.put("ceshiEquipId", soft.getEquipId());
					map.put("ceshiDate", new Date());
					map.put("ceshiHege", 1);
					List<ZsEquipCeshi> equipCeshis  = equipCeshiSercie.findByDateHegeState(map);
					if (!equipCeshis.isEmpty()) {
						rtnMap.put("msg", "扫码成功");
						rtnMap.put("state", "true");
						rtnMap.put("soft", soft);
						rtnMap.put("equip", equip);
					}else if(equip.getEquipCategory()!=null){
						if(182==equip.getEquipCategory()){
							rtnMap.put("msg", "扫码成功");
							rtnMap.put("state", "true");
							rtnMap.put("soft", soft);
							rtnMap.put("equip", equip);
						}else {
							rtnMap.put("msg", "没有进行bd测试");
							rtnMap.put("state", "false");
						}
					}
				}else {
					rtnMap.put("msg", "该设备无此程序");
					rtnMap.put("state", "fasle");
				}
			}else {
				rtnMap.put("msg", "无此程序");
				rtnMap.put("state", "fasle");
			}

		}catch (Exception e){
			rtnMap.put("msg", "无此程序");
			rtnMap.put("state", "fasle");
		}
		return rtnMap;

	}

	@RequestMapping("qingxiSoft.do")
	@ResponseBody
	public Map<Object, Object> qingxiSofts(String softId, HttpServletRequest request,HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<>();
		Map<String, Object> map = new HashMap<>();
		try{
			int sid = Integer.valueOf(softId);
			ZsSoft soft = zsSoftService.selectByPrimaryKey(Integer.valueOf(softId));
			if (soft != null) {
				if(soft.getSoftType()==1){
					ZsEquip equip = equipService.selectByPrimaryKey(soft.getEquipId());
					if (equip != null) {
						rtnMap.put("msg", "扫码成功");
						rtnMap.put("state", "true");
						rtnMap.put("soft", soft);
						rtnMap.put("equip", equip);
					}else {
						rtnMap.put("msg", "该设备无此程序");
						rtnMap.put("state", "fasle");
					}
				}else{
					rtnMap.put("msg", "该程序不是清洗程序");
					rtnMap.put("state", "fasle");
				}
			}else {
				rtnMap.put("msg", "无此程序");
				rtnMap.put("state", "fasle");
			}

		}catch (Exception e){
			rtnMap.put("msg", "无此程序");
			rtnMap.put("state", "fasle");
		}
		return rtnMap;

	}
}